// pages/usercenter/usercenter.js
var Config = require('../../Config');
var Http = require('../../utils/http');
var Utils = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appVersion: '',
    wxAvatar: '',
    userbasicInfo: {
      "pt_name": "", //教练名称
      "mobile": "",//教练手机  /登录账号
      "pt_id": 0,//教练id,
      "shop_id": 0,//门店id
      "shop_name": ''
    }
  },
  getUserLoginInfo() {
    const self = this;
    wx.getStorage({
      key: 'teacherInfo',
      success: function (loginInfo) {
        wx.showLoading({
          title: '获取用户信息中...'
        });
        Http.post('/MyDetail', {
          pt_id: loginInfo.data.pt_id,
          shop_id: loginInfo.data.shop_id
        }, function (res) {
          wx.hideLoading();
          if(res.flag) {
            self.setData({
              "userbasicInfo": Utils.extend(true, { shop_name: loginInfo.data.shop_name }, res.data)
            });
          }
        });
      }
    })
  },
  getWeixinAvatar() {
    const self = this;
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo;
        var avatarUrl = userInfo.avatarUrl;
        self.setData({
          "wxAvatar": avatarUrl
        });
      }
    })
  },
  logout() {
    const self = this;
    wx.showModal({
      title: '提示',
      content: '是否确认退出当前账号？',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorage({
            key: 'teacherInfo',
            success: function (res) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            },
          })
        }
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      'appVersion': Config.APP_VERSION
    });
  },
  onShow(){
    const self = this;
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              // 用户已经同意小程序获取用户信息，后续调用 wx.getUserInfo 接口不会弹窗询问
              self.getWeixinAvatar();
            }
          })
        } else {
          self.getWeixinAvatar();
        }
      }
    });
    self.getUserLoginInfo();
  }
})